package com.yueyang.sojourn.server.common.baymax.web.result.exception;

import com.yueyang.sojourn.server.common.baymax.web.result.RestResult;
import com.yueyang.sojourn.server.common.baymax.web.result.RestResult.Codes;
import com.yueyang.sojourn.server.common.baymax.web.result.StandardRestResult;
import org.springframework.http.HttpStatus;

/**
 * 所有业务接口异常的父类，异常对应不同的Http Status
 * 
 * @author huisman
 * @since 1.0.0
 * @Copyright (c) 2015, yueyang Group All Rights Reserved.
 */
public class AbstractResultException extends RuntimeException{
  private static final long serialVersionUID = 1L;
  private RestResult result;
  private HttpStatus httpStatus;


  /**
   * 异常对应的http 状态吗
   * 
   * @since 1.0.0
   * @return
   */
  public HttpStatus getHttpStatus() {
    return httpStatus;
  }

  /**
   * 标准输出信息
   * 
   * @since 1.0.0
   * @return
   */
  public RestResult getResult() {
    return result;
  }



  /**
   * 指定异常消息提示，以及http 状态
   * 
   * @since 1.0.0
   * @param httpStatus
   * @param message 业务提示
   */
  public AbstractResultException(String message, HttpStatus httpStatus) {
    this(Codes.UNKNOWN, message, httpStatus);
  }

  /**
   * 设置异常对应的Http状态码，业务错误码，业务提示信息
   * 
   * @since 1.0.0
   * @param code 业务错误码
   * @param message 提示信息
   * @param httpStatus 标准http状态码
   */
  public AbstractResultException(int code, String message,
      HttpStatus httpStatus) {
   this(code,message,httpStatus,true);
  }
  
  /**
   * 设置异常对应的Http状态码，业务错误码，业务提示信息，可指定是否生成详细的错误信息
   * @param code 业务错误码
   * @param message 提示信息
   * @param httpStatus 标准http状态码
   * @param withDetailMessage 是否生成详细错误信息
   */
  public AbstractResultException(int code, String message,
      HttpStatus httpStatus,boolean withDetailMessage) {
    //30(constant)+4(httpstatus)+10(code)+20(message)
    super(withDetailMessage?new StringBuilder(65).append("code:")
        .append(code).append(",message:")
        .append(message).append(",responseStatus:")
        .append(httpStatus.value()).toString(): message,
        null, false, false);
    this.result = new StandardRestResult(code, message);
    this.httpStatus = httpStatus;
  }


}
